//
//  BoyeChooseCityView.m
//  itboye2015002
//
//  Created by boye_mac1 on 15/9/15.
//  Copyright (c) 2015年 Boye. All rights reserved.
//

#import "BoyeChooseCityView.h"
#import "CityChooseDataBase.h"
#import "ProvinceDAO.h"
#import "CityDAO.h"
#import "AreaDAO.h"
#import "BoyeViewHandle.h"


@implementation BoyeChooseCityView

-(NSMutableArray *)provinceListArray{
    
    if (_provinceListArray == nil) {
        _provinceListArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _provinceListArray;
}

-(NSMutableArray *)cityListArray{
    
    if (_cityListArray == nil) {
        _cityListArray = [[NSMutableArray alloc] initWithCapacity:0];
        
    }
    return _cityListArray;
}

-(NSMutableArray *)areaListArray{
    
    if (_areaListArray == nil) {
        _areaListArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _areaListArray;
}

-(instancetype)initWithPicker{
    
    self = [super init];
    if (self) {
        
        
        [self _initViews];
        
    }
    return self;
}
//
-(void)_initViews{
    
    [self _initCityData];
    
    [self creatPickerView];
}

// 创建数据
-(void)_initCityData{
    
    ProvinceDAO * provinceDAO = [[ProvinceDAO alloc] init];
    //省
//    [self.provinceListArray setArray: [CityChooseDataBase getAllProvinceModel]];
    
    [self.provinceListArray setArray: [provinceDAO query:nil]];
    
    BoyeProvinceModel  * province = [self.provinceListArray firstObject];
    
    CityDAO * dao = [[CityDAO alloc] init];
    
//城市
//[self.cityListArray setArray:[CityChooseDataBase getAllCityProvinceID:province.provinceID]];
    
    [self.cityListArray setArray: [dao queryByProvinceID:province.provinceID]];
    
    BoyeCityModel * city = [self.cityListArray firstObject];
    
    AreaDAO * areaDAO = [[AreaDAO alloc] init];
    
    //地区
//    [self.areaListArray setArray: [CityChooseDataBase getAllArearCityID:city.cityID]];
    [self.areaListArray setArray: [areaDAO queryByCityID:city.cityID]];
    
    //数据
    self.dataArray =[[NSMutableArray alloc] initWithCapacity:0];
    [self.dataArray addObject:self.provinceListArray];
    [self.dataArray addObject:self.cityListArray];
    [self.dataArray addObject:self.areaListArray];
}

// creatPickerView
-(void)creatPickerView{
    
    if (self.pickerView == nil) {
        //视图
        self.backgroundColor = [UIColor lightGrayColor];
        self.bounds = CGRectMake(0, 0, SCREEN_WIDTH, 240);
        self.center = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT + self.height/2.0 - 64 );
        
        //顶部黑条
        UIView * topView = [[UIView alloc] init];
        topView.frame = CGRectMake(0, 0, self.width, 35);
        topView.backgroundColor = JM_THEME_COLOR;
        [self addSubview:topView];
        
        //顶部线
//        UILabel * topLine = (UILabel *)[BoyeViewHandle defaultLineInView:topView];
//        topLine.center = CGPointMake(topLine.x, 0.5);
        //底部线
//        [BoyeViewHandle defaultLineInView:topView];
        //完成按钮
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button.bounds = CGRectMake(0, 0, 80, topView.height);
        CGFloat  x  =  topView.width  - button.bounds.size.width/2.0;
        button.center = CGPointMake(x, topView.height /2.0);
        [button setTitle:@"完成" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [topView addSubview:button];
        //    [ButtonFactory   decorateButton:button forType:BOYE_BTN_SUCCESS];
        
        
        [button addTarget:self action:@selector(buttonCloseClick) forControlEvents:UIControlEventTouchUpInside];
        
        //down
        UILabel * touchDown = [[UILabel alloc] init];
        touchDown.frame = CGRectMake(0, 0, topView.width - button.width- 2, topView.height);
        touchDown.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];
        [touchDown addGestureRecognizer:tap];
        [topView addSubview:touchDown];
        
        
        //底部
        UIView * downView = [[UIView alloc] init];
        downView.frame = CGRectMake(0, topView.bottom, topView.width, self.height-topView.height);
        [self addSubview:downView];
        
#pragma mark -- PickView
        self.pickerView = [[UIPickerView alloc] init];
        self.pickerView.width = SCREEN_WIDTH;
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        self.pickerView.backgroundColor = [UIColor whiteColor];
        [downView addSubview: self.pickerView];
    }
}


#pragma mark --- pickView --

// 列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 3;
}

//行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  
    return [(NSArray *)[self.dataArray objectAtIndex:component] count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component == 0) {
        BoyeProvinceModel * province = self.provinceListArray[row];
        return province.province;
    }else if (component == 1) {
        
        BoyeCityModel * city = self.cityListArray[row];
        return city.city;
        
    }else{
        
        BoyeAreaModel * area = self.areaListArray[row];

        return area.area;
    }
}

#pragma mark -- 选中之后  --
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    

    
    if (component == 0) {
        self.provinceRow = row;
        
        [self getCityData:row];
        [self getAreaData:0];
        
        self.provinceRow = row;
        self.cityRow = 0;
        self.areaRow = 0;

    }else if (component == 1){
        self.cityRow = row;
        
        [self getAreaData:row];

        self.areaRow = 0;
        
        
    }else{
        self.areaRow = row;
        
    }
    
    [self.pickerView reloadAllComponents];
    
    [self.pickerView selectRow:self.cityRow inComponent:1 animated:YES];
    [self.pickerView selectRow:self.areaRow inComponent:2 animated:YES];
    
    
    if ([_delegate respondsToSelector:@selector(chooseCityView:cityObject:)]) {
        
        CityObject * object = [self buildCityObject];
        
        [_delegate chooseCityView:self cityObject:object];
    }
    
    
}

// 创建模型
-(CityObject *) buildCityObject{
    
    CityObject * object = [[CityObject alloc] init];
    //省
    BoyeProvinceModel * provi = self.provinceListArray[self.provinceRow];
    object.province = provi.province;
    object.provinceid =  provi.provinceID;
    //city
    if (self.cityListArray.count == 0) {
        object.city = @"";
        object.cityid  =  @"";
    }else{
        BoyeCityModel * city = self.cityListArray[self.cityRow];
        object.city = city.city;
        object.cityid  =  city.cityID;
    }
    
    //area
    if (self.areaListArray.count == 0) {
        object.area = @"";
        object.areaid = @"";
    }else{
        BoyeAreaModel * area = self.areaListArray[self.areaRow];
        object.areaid = area.areaID;
        object.area = area.area;
    }
    
    object.pcaAddress =[NSString stringWithFormat:@"%@%@%@",object.province,object.city,object.area];

    return object;
}

#pragma mark  -- 根据provinceID 读取城市列表
-(void) getCityData:(NSInteger )row{
    
    CityDAO * dao = [[CityDAO alloc] init];
    //城市
    BoyeProvinceModel  * province = self.provinceListArray [row];
    [self.cityListArray removeAllObjects];
    
//    [self.cityListArray setArray:[CityChooseDataBase getAllCityProvinceID:province.provinceID]];
    
    [self.cityListArray setArray:[dao queryByProvinceID:province.provinceID]];
    
}
#pragma mark  -- 根据cityID 读取地区列表

-(void)getAreaData:(NSInteger )row{
    
    //地区
    DLog(@"   --  城市ID %ld--  ",(unsigned long)self.cityListArray.count);
    if (self.cityListArray.count == 0) {

        [self.areaListArray removeAllObjects];

        return;
    }

    BoyeCityModel  * city = self.cityListArray[row];
    DLog(@"cityID= %@",city.cityID);
    AreaDAO * dao = [[AreaDAO alloc] init];
    [self.areaListArray removeAllObjects];
    [self.areaListArray setArray:[dao queryByCityID: city.cityID]];
}

#pragma mark -重载delegate --
-(void)setDelegate:(id<BoyeChooseCityViewDelegate>)delegate{
    _delegate = delegate;
}



#pragma mark --  open  --
-(void)open{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    self.center = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT - self.height/2.0 - 64);
    [UIView commitAnimations];
    
    
    self.isOpen = YES;
    [self sendSelfFrameNotification];
    
    
}


#pragma mark --  close  --
-(void)close{
    
//    [BoyeViewHandle setAnimationCentView:<#(UIView *)#> duration:<#(CGFloat)#> pointCent:<#(CGPoint)#>];
    
    [BoyeViewHandle setAnimationCentView:self
                        duration:0.15
                       pointCent:CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT + self.height/2.0 - 64)];
    self.isOpen = NO;
    [self sendSelfFrameNotification];
    
}



#pragma mark --  showInView  --
-(void)showInView:(UIView *)view{
    
    [view addSubview:self];
}


#pragma mark -- 完成  --

-(void)buttonCloseClick{
    [self close];
    
}




-(void)sendSelfFrameNotification{
    NSString  * heightString = [NSString stringWithFormat:@"%f",self.top];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    //postNotificationName发送一个通知，第一个参数是通知的名字，第二个参数是通知的发送者，一般写self，第三个参数是通知的传参。
    [center postNotificationName:@"pickerKey" object:self userInfo:@{@"viewHeightInfo":heightString}];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
